<%@ page import="java.util.List" %>
<%@ page import="com.example.ordersystem.domain.LineitemInfo" %><%--
  Created by IntelliJ IDEA.
  User: 檬zhu
  Date: 2021/12/14
  Time: 19:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/resources/layui/css/layui.css">
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=Lbn1SZBh2NknQy3nUgWl0aspF84KXQma"></script>
</head>

<body>
<jsp:include page="title.jsp"/>
<%
    /*总计消费*/
//    配送费
    int sum = 0;
    if(request.getAttribute("distancebill") != null){
        int distancebill =  (int) Math.round(Double.valueOf(request.getAttribute("distancebill").toString()));
        sum = distancebill;
    }

    Object object = session.getAttribute("items");//获取当前顾客的所有订单项
    List<LineitemInfo> items = null;

    /*订单项是否为空*/
    int status = 0;



    if (object == null) {
        status = -1;
    }else {
        items = (List<LineitemInfo>)object;
        if (items.size() == 0)
            status = -1;
    }

    if (status == -1) {
%>
<jsp:include page="/customer/nothing"/>
<%
        return;    //不再执行接下来的代码
    }
%>
<form class="layui-form layui-form-pane" action="${pageContext.request.contextPath}/customer/customeraddress" method="post"
      style="margin-left:1px;float: left">
    <div class="layui-form-item">
        <label class="layui-form-label">收货地址</label>
        <div class="layui-input-inline" id="r-result">
            <input type="text" name="address" lay-verify="title" id="suggestId"
                   placeholder="请输入收货地址" autocomplete="off" class="layui-input">
        </div>
        <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
    </div>
    <div id="l-map" style="height: 400px;width: 600px;margin-bottom: 10px"></div>

    <input type="hidden" name="lng" id="lng" value="">
    <input type="hidden" name="lat" id="lat" value="">
    <div class="layui-form-item">
        <div class="layui-input-block" style="margin-left: 0">
            <button type="reset" class="layui-btn layui-btn-radius layui-btn-primary">重置</button>
            <button type="submit" class="layui-btn layui-btn-radius" data-type="content" lay-submit="">立即提交</button>
        </div>
    </div>
</form>
<div style="margin-top: 3%;margin-left: 50%;width: 610px;height: 450px;overflow: auto">
    <form class="layui-form layui-form-pane" action="form.html">
    <div class="layui-form-item">
        <label class="layui-form-label">配送费</label>
        <div class="layui-input-inline">
            <input type="text" lay-verify="required" value="<%=sum%>" disabled="disabled" autocomplete="off" class="layui-input">
        </div>
    </div>
    </form>
    <table class="layui-table" style="width: 600px; float: left;">
        <thead>
        <tr>
            <th>菜品</th>
            <th>图片</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        <%
            for (int i = 0; i < items.size(); ++i) {
                sum += items.get(i).getPrice() * items.get(i).getQuantity();
        %>
        <tr>
            <td><%=items.get(i).getdName()%>
            </td>
            <td><img alt="" src="<%=items.get(i).getDisplayPicture()%>"></td>
            <td><%=items.get(i).getPrice()%>
            </td>
            <td><%=items.get(i).getQuantity()%>
            </td>
            <td><%=items.get(i).getAmount()%>
            </td>
            <td>
                <form action="${pageContext.request.contextPath}/customer/deleteDishitem"
                      onSubmit="return window.confirm('确认删除吗？')" method="post">
                    <input type="hidden" name="index" value="<%=i%>">
                    <button class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger">删除</button>
                </form>
            </td>
                <%
					}
				%>

        </tbody>
    </table>
    <textarea class="layui-textarea" id="LAY_demo1" style="display: none;margin-top: 5%;">给老板留言</textarea>
</div>
<div class="layui-input-block" style="margin-left: 50%;margin-top: 13px">
    <form action="${pageContext.request.contextPath}/customer/checkoutPage" method="post">
        <input type="hidden" id="input" name="remark">
        <input type="hidden" name="total" value="<%=sum%>">
        <button class="layui-btn layui-btn-radius layui-btn-primary" type="button">共计<%=sum%>
        </button>
        <button class="layui-btn layui-btn-radius" data-type="content" lay-submit="" type="submit">立即结账</button>
    </form>
</div>
<script src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form, layer = layui.layer, layedit = layui.layedit, laydate = layui.laydate, $ = layui.jquery;

        //构建一个默认的编辑器
        var index = layedit.build('LAY_demo1', {
            tool: ['face', 'link', 'unlink', '|', 'left', 'center',
                'right'],
            height: 100
        });

        //编辑器外部操作
        var active = {
            content: function () {
                console.log(layedit.getContent(index)); //获取编辑器内容
                layedit.sync(index); //同步编辑器中的内容
                document.getElementById("input").innerText = layedit.getContent(index); //给hidden赋值
                document.getElementById("input").value = layedit.getContent(index); //给hidden赋值，两次赋值是为了解决浏览器兼容问题
            }
        };

        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>

<script type="text/javascript">
    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }

    var map = new BMap.Map("l-map");
    map.centerAndZoom("江门",12);                   // 初始化地图,设置城市和地图级别。

    map.enableScrollWheelZoom(true);
    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
        {"input" : "suggestId"
            ,"location" : map
        });

    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

        setPlace();
    });

    function setPlace(){
        map.clearOverlays();    //清除地图上所有覆盖物
        function myFun(){
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            document.getElementById("lng").value = pp.lng;
            document.getElementById("lat").value = pp.lat;
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMap.Marker(pp));    //添加标注
        }
        var local = new BMap.LocalSearch(map, { //智能搜索
            onSearchComplete: myFun
        });
        local.search(myValue);
    }
</script>
